package com.ksd.modules.adminlogs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.convert.CopyConvertUtils;
import com.ksd.modules.adminlogs.entity.SysLog;
import com.ksd.modules.adminlogs.mapper.SysLogMapper;
import com.ksd.modules.adminlogs.po.SysLogSavePO;
import com.ksd.modules.adminlogs.po.SysLogSearchPO;
import com.ksd.modules.adminlogs.service.ISysLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 系统日志 服务实现类
 * </p>
 *
 * @author Auto Generator
 * @since 2023-08-16
 */
@Service
public class SysLogService extends BaseServiceImpl<SysLogMapper, SysLog> implements ISysLogService {

    @Override
    public Page<SysLog> findSysLog(SysLogSearchPO sysLogSearchPO) {
        LambdaQueryWrapper<SysLog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysLog::getSystemId, SystemIdThrealLocal.get().getSystemId());
        if (StringUtils.isNotEmpty(sysLogSearchPO.getKeyword())) {
            lambdaQueryWrapper.and(c -> c.like(SysLog::getUsername, sysLogSearchPO.getKeyword())
                    .or().like(SysLog::getMethod, sysLogSearchPO.getKeyword())
                    .or().like(SysLog::getClassName, sysLogSearchPO.getKeyword())
                    .or().like(SysLog::getDescription, sysLogSearchPO.getKeyword()));
        }
        lambdaQueryWrapper.orderByDesc(SysLog::getCreateTime);
        // 查询返回
        Page<SysLog> page = new Page<>(sysLogSearchPO.getPageNo(), sysLogSearchPO.getPageSize());
        return this.page(page, lambdaQueryWrapper);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveUpdateSysLog(SysLogSavePO sysLogSavePO) {
        SysLog sysLog = CopyConvertUtils.copyProperties(sysLogSavePO, SysLog.class);
        sysLog.setCreateTime(LocalDateTime.now());
        return this.saveOrUpdate(sysLog);
    }

    @Override
    public boolean deleteSysLogById(Long id) {
        return this.removeById(id);
    }

    @Override
    public SysLog getSysLogById(Long id) {
        return this.getById(id);
    }
}
